package cn.edu.szpt.qqdemov3.adapters;

import android.content.ContentResolver;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.InputStream;
import java.util.List;
import java.util.Map;

import cn.edu.szpt.qqdemov3.R;
import cn.edu.szpt.qqdemov3.beans.QQContactBean;

public class QQContactAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<String> groupdata;
    private Map<String, List<QQContactBean>> childdata;

    public QQContactAdapter(Context context, List<String> groupdata, Map<String, List<QQContactBean>> childdata) {
        this.context = context;
        this.groupdata = groupdata;
        this.childdata = childdata;
    }

    @Override
    public int getGroupCount() {
        return groupdata.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        List<QQContactBean> list = childdata.get(groupdata.get(groupPosition));
        return list.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupdata.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        List<QQContactBean> list = childdata.get(groupdata.get(groupPosition));
        return list.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_contact_group, parent, false);
            holder = new GroupHolder();
            holder.tv_grouptitle = convertView.findViewById(R.id.tv_grouptitle);
            holder.tv_groupcount = convertView.findViewById(R.id.tv_groupcount);
            convertView.setTag(holder);
        } else {
            holder = (GroupHolder) convertView.getTag();
        }
        String countryname = groupdata.get(groupPosition);
        holder.tv_grouptitle.setText(countryname);
        holder.tv_groupcount.setText(getChildrenCount(groupPosition) + "");
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_contact_child, parent, false);
            holder = new ChildHolder();
            holder.img_qqicon = convertView.findViewById(R.id.img_qqicon);
            holder.tv_qqname = convertView.findViewById(R.id.tv_qqname);
            holder.tv_onlinemode = convertView.findViewById(R.id.tv_onlinemode);
            holder.tv_qqaction = convertView.findViewById(R.id.tv_qqaction);
            convertView.setTag(holder);
        } else {
            holder = (ChildHolder) convertView.getTag();
        }
        QQContactBean bean = (QQContactBean) getChild(groupPosition, childPosition);
        Bitmap bmp;
        if (groupdata.get(groupPosition).equals("本机联系人")) {
            ContentResolver cr = context.getContentResolver();
            InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(cr, Uri.parse(bean.getQq_imgurl()));
            bmp = BitmapFactory.decodeStream(is);
        } else {
            bmp = BitmapFactory.decodeFile(bean.getQq_imgurl());
        }
        holder.img_qqicon.setImageBitmap(bmp);

        holder.tv_qqname.setText(bean.getQq_name());
        holder.tv_onlinemode.setText(bean.getQq_onlinemode());
        holder.tv_qqaction.setText(bean.getQq_action());
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    static class GroupHolder {
        TextView tv_grouptitle;
        TextView tv_groupcount;
    }

    static class ChildHolder {
        ImageView img_qqicon;
        TextView tv_qqname;
        TextView tv_onlinemode;
        TextView tv_qqaction;
    }
}
